<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Controller.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Widget_CategoriesSitepageController extends Engine_Content_Widget_Abstract {

  public function indexAction() {
  	
    $this->view->sitepage_categories = $sitepage_categories = Zend_Registry::isRegistered('sitepage_categories') ? Zend_Registry::get('sitepage_categories') : null;    
    
    //SET NO RENDER
    if (empty($sitepage_categories)) {
      return $this->setNoRender();
    }    
    
    //GET CORE CONTENT TABLE
    //$contentTable = Engine_Api::_()->getDbtable('content', 'core');
    
//    //GET CORE PAGE TABLE
//    $pageTable = Engine_Api::_()->getDbtable('pages', 'core');
//    $selectPage = $pageTable->select()->from($pageTable->info('name'), array('page_id'))->where('name =?', 'sitepage_index_home')->orWhere('name =?', 'sitepage_index_index')->limit(1);
//    $row = $pageTable->fetchRow($selectPage);
//    if (!empty($row)) {
//       $contentTablePage = $contentTable->select()
//                ->from($contentTable->info('name'), array('page_id'))
//                ->where('name =?', 'sitepage.search-sitepage')
//                ->where('page_id =?', $row->page_id)
//                ->limit(1);
//        $contentinfo = $contentTable->fetchRow($contentTablePage);
//        if (!empty($contentinfo)) {
//          $this->view->formsubmit = 0;
//        } else {
//          $this->view->formsubmit = 1;
//        }
//    }

    $categories = array();
    $category_info = Engine_Api::_()->getDbTable('categories', 'sitepage')->getCategories(1);
    foreach ($category_info as $value) {
      $sub_cat_array = array();
      $category_info2 = Engine_Api::_()->getDbtable('categories', 'sitepage')->getAllCategories($value['category_id'], 'subcategory_id', 0, 'subcategory_id', 0);
      //$subcategory_info = Engine_Api::_()->getDbTable('categories', 'sitepage')->getSubCategories($value['category_id']);
      foreach($category_info2 as $subresults) {
        $treesubarray = array();
        $subcategory_info2 = Engine_Api::_()->getDbtable('categories', 'sitepage')->getAllCategories($subresults['category_id'], 'subcategory_id', 0, 'subcategory_id', 0);
        $treesubarrays[$subresults->category_id] = array();
        foreach($subcategory_info2 as $subvalues) {
           $treesubarray = array('tree_sub_cat_id' => $subvalues->category_id,
            'tree_sub_cat_name' => $subvalues->category_name,
            'order' => $subvalues->cat_order,
            );
           $treesubarrays[$subresults->category_id][] = $treesubarray;
        }

        $tmp_array = array('sub_cat_id' => $subresults->category_id,
            'sub_cat_name' => $subresults->category_name,
            'tree_sub_cat' => $treesubarrays[$subresults->category_id],
            'order' => $subresults->cat_order);
        $sub_cat_array[] = $tmp_array;
      }

      $category_array = array('category_id' => $value->category_id,
          'category_name' => $value->category_name,
          'order' => $value->cat_order,
          'sub_categories' => $sub_cat_array);
      $categories[] = $category_array;
    }
    
    $this->view->categories = $categories;
    $this->view->subcategorys = 0;
    $this->view->category = 0;
    $this->view->subsubcategorys = 0;
    $categoryname = Zend_Controller_Front::getInstance()->getRequest()->getParam('categoryname', null);
    $subcategoryname = Zend_Controller_Front::getInstance()->getRequest()->getParam('subcategoryname', null);
    $subsubcategoryname = Zend_Controller_Front::getInstance()->getRequest()->getParam('subsubcategoryname', null);

    if(Zend_Controller_Front::getInstance()->getRequest()->getParam('category') ) {
	
	    $categoryidtemp = Zend_Controller_Front::getInstance()->getRequest()->getParam('category');	
	     if(Zend_Controller_Front::getInstance()->getRequest()->getParam('subcategory')) {
        $subcategoryidtemp = Zend_Controller_Front::getInstance()->getRequest()->getParam('subcategory');
      } else {
        $subcategoryidtemp = Zend_Controller_Front::getInstance()->getRequest()->getParam('subcategory_id');
      }
      if(Zend_Controller_Front::getInstance()->getRequest()->getParam('subsubcategory')) {
        $subsubcategoryidtemp = Zend_Controller_Front::getInstance()->getRequest()->getParam('subsubcategory');
      } else {
        $subsubcategoryidtemp = Zend_Controller_Front::getInstance()->getRequest()->getParam('subsubcategory_id');
      }
	    if(!empty($categoryidtemp)) {
	    	$this->view->category = $categoryidtemp; 
	    	$this->view->subcategorys = $subcategoryidtemp;
        $this->view->subsubcategorys = $subsubcategoryidtemp;
	    }
    } elseif(Zend_Controller_Front::getInstance()->getRequest()->getParam('category_id')) {
	    $categoryid = Zend_Controller_Front::getInstance()->getRequest()->getParam('category_id');
	    $subcategoryid = Zend_Controller_Front::getInstance()->getRequest()->getParam('subcategory_id');
      $subsubcategoryid = Zend_Controller_Front::getInstance()->getRequest()->getParam('subsubcategory_id');
	
	    if (!empty($categoryid)) {
	      $_GET['category_id'] = $this->view->category = $categoryid;
	      $_GET['categoryname'] = $categoryname;
	    } 
	    
	    if (!empty($subcategoryid)) {  
	      $_GET['subcategory_id'] = $this->view->subcategorys = $subcategoryid;
	      $_GET['subcategoryname'] = $subcategoryname;
	    }
      
      if (!empty($subsubcategoryid)) {
	      $_GET['subsubcategory_id'] = $this->view->subsubcategorys = $subsubcategoryid;
	      $_GET['subsubcategoryname'] = $subcategoryname;
	    }

	    if (!empty($_GET)) {
	      if (!empty($_GET['subcategory_id'])) {
	        $this->view->subcategorys = $_GET['subcategory_id'];
	      }
	      if (!empty($_GET['category_id'])) {
	        $this->view->category = $_GET['category_id'];
	      }
        if (!empty($_GET['subsubcategory_id'])) {
	        $this->view->subsubcategorys = $_GET['subsubcategory_id'];
	      }
	    }
    }
    if(empty($categoryname)) {
      $_GET['category'] = $this->view->category_id =  $this->view->category = 0;
			$_GET['subcategory'] = $this->view->subcategory_id = 0;
      $_GET['subsubcategory'] = $this->view->subsubcategory_id = 0;
			$_GET['categoryname'] = 0;
			$_GET['subcategoryname'] = 0;
      $_GET['subsubcategoryname'] = 0;
    }
       
    if (!(count($this->view->categories) > 0)) {
      return $this->setNoRender();
    }

  }

}

?>